//
//  MQWelcomeViewController.swift
//  SinaWeiBo
//
//  Created by 毛强 on 16/1/19.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SDWebImage

class MQWelcomeViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        if let headImageUrlString = MQUserAccountViewModel.shareUserAccountViewModel.userAccount?.avatar_lager{
            headImage.sd_setImageWithURL(NSURL(string: headImageUrlString), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        setupUI()
    }
    
    private func setupUI(){
        view.backgroundColor = UIColor(white: 247/255, alpha: 1)
        view.addSubview(headImage)
        view.addSubview(messageLabel)
        
        //设置约束
        headImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSizeMake(90, 90))
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(headImage.snp_bottom).offset(10)
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        headImage.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(100)
        }
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            
            }) { (Bool) -> Void in
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                }, completion: { (Bool) -> Void in
                    //发送通知切换控制器
                    printfLog("登录成功")
                     NSNotificationCenter.defaultCenter().postNotificationName(MQSwitchRootViewControllerNotification, object: nil)
                })
        }
    }
    
    // MARK: - 懒加载
    private lazy var headImage:UIImageView = {
        let image =  UIImageView(image: UIImage(named: "avatar_default_big"))
        image.layer.cornerRadius = 45
        image.layer.masksToBounds = true
        return image
    }()
   
    private lazy var messageLabel:UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 16)
        label.text = "欢迎回来"
        label.alpha = 0
        return label
    }()
}
